#!/usr/bin/env python
#!/usr/bin/env python
"""
Acorn-SDR (A Collaboration of Radio Nodes)
(c) Bob Cowdery (G3UKB) 2009

File: gtk_mini_gui.py
Date: January 2009

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

A bootstrap GUI using GTK+.

"""

#-----------------------------------------------------------------------
# System imports
import sys
from time import sleep

# Import GTK libraries
try:
  import pygtk
  #tell pyGTK, if possible, that we want GTKv2
  pygtk.require("2.0")
except:
  #Some distributions come with GTK2, but not pyGTK
  pass
try:
  import gtk
  import gtk.glade
  import gobject
except:
  print "You need to install pyGTK or GTKv2 ",
  print "or set your PYTHONPATH correctly."
  print "try: export PYTHONPATH=",
  print "/usr/local/lib/python2.2/site-packages/"
  sys.exit(1)

# Application imports
import utils

#======================================================================
# The GUI class. There will be other classes downstream.
class BootstrapGui:
  
    #---------------------------------------------------------------------
    def __init__(self, glade_path, persist_service):
    
        self.persist = persist_service
        # Allow threads, without this the gui will hang all python threads
        # We don't actually use any Python threads right now but its very
        # disconcerting if you do and find they block, so leave this in
        gtk.gdk.threads_init()
        # Initialise the glade built ui
        # Load the main window
        windowname = "ProfileSelection"
        # This loads a widget tree from the XML definition in the glade file
        self.main_tree=gtk.glade.XML (glade_path, windowname)
        # Get and show the main window
        w=self.main_tree.get_widget(windowname)
        w.show_all()
    
        # Wire up the top level window events
        # All events are set up in Glade, just match the event names here
        dic = {
            "on_cb_profiles_changed" : self.profile_changed,
            "on_b_cancel_clicked" : self.cancel,
            "on_b_load_clicked" : self.load,   
        }
    
        # This will connect up all the events in one hit
        self.main_tree.signal_autoconnect (dic)
    
        # Now we need a reference to all the controls
        # A bit long winded but easy to follow
        # All controls in the tree must have a unique name
        self.profiles = self.main_tree.get_widget('cb_profiles')
        self.cancel = self.main_tree.get_widget('b_cancel')
        self.load = self.main_tree.get_widget('b_load')
        self.load.set_sensitive(False)        
        self.tv_profile = self.main_tree.get_widget('tv_profile')
    
        # Populate the dropdown
        liststore = gtk.ListStore(gobject.TYPE_STRING)
        self.profiles.set_model(liststore)
        cell = gtk.CellRendererText()
        self.profiles.pack_start(cell, True)
        self.profiles.add_attribute(cell, 'text', 0)  
        profiles = self.persist.getProfiles()
        profile_list = profiles.rsplit(',')
        for profile in profile_list:
            if profile <> 'Bootstrap':
                self.profiles.append_text(profile)
        
        # Create a text buffer for the text view
        self.textbuffer = gtk.TextBuffer(table=None)
        self.tv_profile.set_buffer(self.textbuffer)

        self.profile = None
    
    #---------------------------------------------------------------------
    # GUI generated events (i.e. things that happen when you play with widgets)
  
    #=====================================================================
    # Events for the main window
    def delete_event(self, widget, event, data=None):
     # If you return FALSE in the "delete_event" signal handler,
        # GTK will emit the "destroy" signal. Returning TRUE means
        # you don't want the window to be destroyed.
        # This is useful for popping up 'are you sure you want to quit?'
        # type dialogs.

        # Change FALSE to TRUE and the main window will not be destroyed
        # with a "delete_event".
        return False

    #---------------------------------------------------------------------
    def destroy(self, widget, data=None):
        self.ret = -1
        gtk.main_quit()
  
    #---------------------------------------------------------------------
    def profile_changed(self, widget):
        self.profile = self.get_active_text(widget)
        self.persist.setProfile(self.profile)
        description = utils.persist_get(self.persist, 'Profile', 'description')
        self.textbuffer.set_text(description)
        self.load.set_sensitive(True)

    #---------------------------------------------------------------------
    def cancel(self, widget):
        self.ret = -1
        gtk.main_quit()
        
    #---------------------------------------------------------------------
    def load(self, widget):
        self.ret = 0
        gtk.main_quit()

    #=====================================================================
    # Private    
    def get_active_text(self, combobox):
        model = combobox.get_model()
        active = combobox.get_active()
        if active < 0:
            return None
        return model[active][0]
      
    #=====================================================================
    # Entry point
    def main(self):
        # All PyGTK applications must have a gtk.main(). Control ends here
        # and waits for an event to occur (like a key press or mouse event)
        gtk.main()
        # Return here when the UI is closed
        return self.ret

